/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#include "EntityComponentFactory.h"
#include "EntityComponentMaker.h"
#include "../XUtilities/Utilities.h"

namespace XF
{
	EntityComponentFactory::EntityComponentFactory()
	{
	}

	EntityComponentFactory::~EntityComponentFactory()
	{
	}

	void EntityComponentFactory::RegisterMaker(std::string id, EntityComponentMakerBase* maker)
	{
		std::map<std::string, EntityComponentMakerBase*> *makersList = MakersList();

		Utilities::Assert((makersList->find(id) == makersList->end()), "EntityComponentFactory::RegisterMaker: Attempting to register maker with existing id: " + id);

		makersList->insert(std::make_pair(id, maker));
	}

	void EntityComponentFactory::UnregisterMaker(std::string id)
	{
		std::map<std::string, EntityComponentMakerBase*> *makersList = MakersList();

		Utilities::Assert((makersList->find(id) != makersList->end()), "EntityComponentFactory::UnregisterMaker: Attempting to unregister maker with id that doesn't exist: " + id);

		makersList->erase(id);

	}

	EntityComponent* EntityComponentFactory::CreateComponent(std::string id, GameEntity* owner)
	{
		std::map<std::string, EntityComponentMakerBase*> *makersList = MakersList();

		std::map<std::string, EntityComponentMakerBase*>::iterator it = makersList->find(id);

		Utilities::Assert((it != makersList->end()), "EntityComponentMakerBase::CreateProperty: Attempting to create property with unknown id: " + id);

		return (*it).second->MakeComponent(owner);
	}

	std::map<std::string, EntityComponentMakerBase*>* EntityComponentFactory::MakersList()
	{
		static std::map<std::string, EntityComponentMakerBase*> *makersList 
			= new std::map<std::string, EntityComponentMakerBase*>();

		return makersList;
	}
}
